/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.Random;

/**
 * 
 * @author haoliang.donghl
 */
public class GenerateForecast {

	static float[] numArray;
	static float[] initArray;
	static int start;
	static int end;
	static int record;

	static float WAVE_COFF = 0.00f;
	static float WEIGHT_COFF = 0.00f;
	static int APP_NUM = 1;
	static String START_STR = "2009-01-01 00:00:00";
	static String RECORD_STR = "2009-06-30 22:40:00";
	static String END_STR = "2009-07-01 00:00:00";
	static int INTERVAL = 60 * 60;
	static int BUFFER = 100;
	static float PV_CPU = 1;

	static float WEIGHT = 1.5f;
	static float[] WEIGHTS = { 1.0f, 1.1f, 1.2f, 1.3f, 1.4f, 1.5f, 1.6f, 1.7f,
			1.8f, 1.9f };

	static BufferedWriter bw;
	static Random random = new Random();
	static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	private static void readFile(String filename) {
		try {
			Scanner scanner = new Scanner(new File(filename));
			String fileContent = scanner.nextLine();
			String[] strArray = fileContent.split(",");
			initArray = new float[strArray.length];
			for (int i = 0; i < initArray.length; i++) {
				initArray[i] = Float.parseFloat(strArray[i]);
			}
			scanner.close();
		} catch (FileNotFoundException e) {
			System.out.println(e);
		}
	}

	private static void randomWeights() {
		int times = ((end - start) / INTERVAL + 1) / initArray.length + 1;
		WEIGHTS = new float[times];
		for (int i = 0; i < times; i++) {
			WEIGHTS[i] = (float) (1 + random.nextGaussian() * WEIGHT_COFF);
		}
	}

	private static void randomNumber(int index) {
		float weight;
		if (WEIGHTS == null) {
			weight = WEIGHT;
		} else {
			if (index < WEIGHTS.length) {
				weight = WEIGHTS[index];
			} else {
				weight = WEIGHT;
			}
		}
		for (int i = 0; i < initArray.length; i++) {
			int k = i + index * initArray.length;
			numArray[k] = (initArray[i] + ((float) random.nextGaussian())
					* WAVE_COFF * initArray[i])
					* weight;
			// numArray[k] = initArray[i];
			if (numArray[k] < 0)
				numArray[k] = 0;
		}
	}

	private static void outputData(String appName) {
		try {
			bw = new BufferedWriter(new FileWriter(appName + "_data.txt"));
		} catch (IOException ex) {
			ex.printStackTrace();
		}

		try {
			int temp = (record - start) / INTERVAL - BUFFER;
			if (temp < 0)
				temp = 0;
			System.out.println(appName + ":" + "test data time:"
					+ (start + temp * INTERVAL));
			for (int i = temp; i < numArray.length - 1; i++) {
				bw.write(String.valueOf(numArray[i]) + ",");
			}
			bw.write(String.valueOf(numArray[numArray.length - 1]));
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			bw.flush();
			bw.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	private static void init() {
		try {
			Date s = df.parse(START_STR);
			Date r = df.parse(RECORD_STR);
			Date e = df.parse(END_STR);
			start = (int) (s.getTime() / 1000);
			record = (int) (r.getTime() / 1000);
			end = (int) (e.getTime() / 1000);

			int temp = (record - start) / (INTERVAL * initArray.length) + 1;
			temp = start + temp * initArray.length * INTERVAL;
			if ((temp - record) < 30 * 60) {
				temp += initArray.length * INTERVAL;
			}
			record = temp;

			System.out.println("start:" + start);
			System.out.println("record:" + record);
			System.out.println("end:" + end);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		DB db = DB.newInstance();
		db.connect();
		db.clearAppHistory();

		readFile("alisoft.history");
		init();

		for (int i = 0; i < APP_NUM; i++) {
			randomWeights();
			String appName = "yy" + (i + 1);
			System.out.println(appName + ":start insert");

			db.insertApp(appName);
			App app = db.getApp(appName);
			System.out.println(appName + ":insert app");

			numArray = new float[WEIGHTS.length * initArray.length];

			System.out.println(appName + ":compute data");
			for (int j = 0; j < WEIGHTS.length; j++) {
				randomNumber(j);
			}
			System.out.println(appName + ":weights length " + WEIGHTS.length);
			System.out.println(appName + ":data length " + numArray.length);
			System.out.println(appName + ":compute data complete");

			System.out.println(appName + ":insert history data");
			db.insertAppHistory(app, numArray, start, record, INTERVAL, PV_CPU);
			System.out.println(appName + ":insert history data complete");

			// System.out.println(appName + ":output forecast data");
			// outputData(appName);
			// System.out.println(appName + ":output forecast data");
		}
		db.close();

	}

}
